fix: apply custom Map results when mapping to existing target (#900)#970
fix: apply custom Map results when mapping to existing target (#900)#970leno23 wants to merge 1 commit into
Conversation
…rMapper#900) MapToTarget was passing the existing destination member into nested adaptation even when a custom .Map() resolver already computed the final value, causing immutable/getter-only member values to be preserved. Co-authored-by: Cursor <cursoragent@cursor.com>
| config.NewConfig<Dto900, Entity900>() | ||
| .Map(x => x.Data, x => x.Data == null ? null : new Data900(x.Data.Value)); |
There was a problem hiding this comment.
@andrerav @stagep
I think users want something like this.
config.NewConfig<Poco, Dto>()
.Map(dto => dto.Age, src => src.AgeString)
.OvverideTypesConfigForMapPair(cfg=>
cfg.DefaultValue("42"));
But Map doesn't override the type mapping behavior; it can simply provide another alternative source.
Example For this case:
config.NewConfig<Dto900, Entity900>()
.Map(x => x.Data, x => x.Data )
.OvverideTypesConfigForMapPair(cfg=>
cfg.MapWith(x => x.Data == null ? null : new Data900(x.Data.Value));
|
Fixes #900 — custom |
|
Thanks @DocSvartz — This PR stays intentionally minimal: #900 reports that an explicit Happy to pivot to a config-override API if maintainers prefer that direction — the regression tests should still apply either way. |
Summary
MapToTarget(mapper.Map(dto, existingEntity)) ignoring custom.Map()resolvers when the destination member already has a value..Map()configuration, skip passing the existing destination member into nested adaptation so the resolver result is assigned directly.Root cause
For
MapToTarget,ClassAdapteralways passed the existing destination member expression intoCreateAdaptExpression. That triggered nestedMapToTargetadaptation for the member type. For immutable/getter-only types (like primary-constructor classes), nested mapping preserves the existing destination instance instead of replacing it with the custom.Map()result.Test plan
Mapster.TestsWhenMappingGetterOnlyMapToTargetRegression.MapToTarget_WithCustomMap_ShouldReplaceExistingDestinationMemberValueFixes #900